//Page2nd1.java: Error free, action listeners for edit btn and save btn of inner boxes implemented // but still not satisfactory IMPLEMENTATION //Page2nd2.java: Added Scrollbar //Page2nd3.java: added another BorderLayout inside previously existing one. //Page2nd4.java: class NotSavedInnerBox and class SavedInnerBox being derived from abstract class InnerBox // so that objects of former 2 classes can be referenced by abstract class InnerBox instance. //Page2nd5.java: modified class SavedInnerBox where 'setEnabled(false)' buttons viz. btn1, btn2, btn3 // replaced by 'JLabel' with same names. //Page2nd6.java: Added border to each inner box. //Page2nd7.java: synchronised with Page1(file TT_GUI16.java) //Page2nd8.java: errors are there, maybe because JPanel[][] is used //TO BE IMPLEMENTED //Page2nd_.java: Using public void addLayoutComponent(String name, Component comp) and // public void removeLayoutComponent(Component comp) defined in class GridLayout // so that a specific innerBox can be re-painted on event generated by its btn4 button. import java.sql.*; import java.util.regex.*; import java.util.*; import java.io.*; import java.awt.*; import javax.swing.*; import javax.swing.border.*; import java.awt.event.*; public class Page2nd extends JFrame// implements ActionListener { public static void main(String[] args) { Page2nd p1; p1= new Page2nd(); } Page2nd() { //Frame Details this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); setTitle("Administrator Screen, Page1/2"); //this.setSize(600,700); //Header and Footer btnHead.setEnabled(false); btnFoot.setEnabled(false); add(btnHead, BorderLayout.NORTH); btnHead.setText("Welcome to page 2"); add(btnFoot, BorderLayout.SOUTH); btnFoot.setText("Waiting for any event"); //BORDERLayout.CENTER JPanel centerPanel = new JPanel(); centerPanel.setLayout(new BorderLayout());//GridBagLayout()); JPanel row1Panel = new JPanel(); //row1Panel.setBounds(1,25,1024,40); JPanel row7Panel = new JPanel(); //row1Panel.setBounds(1,700,1024,40); centerPanel.add(row1Panel, BorderLayout.NORTH);//new GBC(0,0,1,1).setWeight(100,100)); centerPanel.add(Page2nd.scrollPane, BorderLayout.CENTER);//new GBC(0,1,1,5).setWeight(100,100).setIpad(900,600)); centerPanel.add(row7Panel, BorderLayout.SOUTH);//new GBC(0,6,1,1).setWeight(100,100)); add(centerPanel, BorderLayout.CENTER); //row1 components batch //new myFrame(); String[] cbOptions; //row1 batch JComboBox bCBox; JLabel bLabel = new JLabel("Batch: "); cbOptions = myFrame.initialOptions("Batch", "id"); bCBox = new JComboBox(cbOptions); bCBox.addActionListener(this.addListener("Batch"));//, (String)(bCBox.getSelectedItem()))); //row1 room JComboBox rCBox; JLabel rLabel = new JLabel("Room: "); cbOptions = myFrame.initialOptions("Room", "id"); rCBox = new JComboBox(cbOptions); rCBox.addActionListener(this.addListener("Room"));//, (String)(bCBox.getSelectedItem()))); //row1 teacher JComboBox tCBox; JLabel tLabel = new JLabel("Teacher: "); cbOptions = myFrame.initialOptions("Teacher", "id"); tCBox = new JComboBox(cbOptions); tCBox.addActionListener(this.addListener("Teacher"));//, (String)(bCBox.getSelectedItem()))); //row1 row1Panel.add(bLabel); row1Panel.add(bCBox); row1Panel.add(rLabel); row1Panel.add(rCBox); row1Panel.add(tLabel); row1Panel.add(tCBox); //row7 JButton btnBack = new JButton("Back"); btnBack.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { //new myFrame();//page1rowTypeA.viewPage1(); //dispose(); page1rowTypeA.viewPage1(); } } ); row7Panel.add((JButton)(btnBack)); //row7Panel.add(new JButton("go")); //row7Panel.add(new JButton("print")); //row 2 to 6 = gridPanel //Page2nd.gridPanel.add(new JButton("gridPanel here")); pack(); } public ActionListener addListener(final String tableName)//for the 3 JComboBoxes in row1 { return(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox cbox = (JComboBox)(e.getSource()); String item = (String)(cbox.getSelectedItem()); if((item.charAt(0)) != '-')//(!(item.charAt(0)).equals("-")) { try { ParticularTT ptt = new ParticularTT(tableName, item); (Page2nd.gridPanel).removeAll(); (Page2nd.gridPanel).add(ptt.getPanel()); } catch(SQLException error) { System.out.println("myERROR, in actionPerformed()"+error.toString()); } (Page2nd.gridPanel).repaint(); pack(); } } }); } static JPanel gridPanel = new JPanel(); static JScrollPane scrollPane = new JScrollPane(gridPanel); static JButton btnHead = new JButton(); static JButton btnFoot = new JButton(); } class ParticularTT { ParticularTT(String tableName, String idName) throws SQLException//constructor { try//initialising a statement object { ParticularTT.con = new JdbcConnection(); ParticularTT.c = con.conn(); ParticularTT.stmt = c.createStatement(); } catch(SQLException err) { System.out.println("a.myError in isEntrySaved()"+err.toString()); } System.out.println(idName+" selected from "+tableName); String[] tableNames = new String[3]; int count=0; tableNames[count] = "Course"; for(String str : new String[]{"Batch","Room","Teacher"}) { if(!tableName.equals(str)) { count++; tableNames[count] = str; System.out.println(str+"added to tableNames[]"); } } pTTPanel = new JPanel(); pTTPanel.setLayout(new GridLayout(5,9)); InnerBox[][] box = new InnerBox[9][5]; for(int d=1; d<=5; d++) for(int p=1; p<=9; p++) { ResultSet rSet = this.isEntrySaved(d, p, tableName, idName, tableNames); if(!(rSet.next()))//give options to save { box[p-1][d-1] = new NotSavedInnerBox(); (((NotSavedInnerBox)(box[p-1][d-1])).btn4).addActionListener(addListener(((NotSavedInnerBox)(box[p-1][d-1])), d, p, tableName, idName, tableNames)); String[] available = myFrame.initialOptions(tableNames[0], "id"); available[0] = "-Select "+tableNames[0]+"-"; for(String str : available) (((NotSavedInnerBox)(box[p-1][d-1])).CBox1).addItem(str); available = findAvailList(tableNames[1]); for(String str : available) (((NotSavedInnerBox)(box[p-1][d-1])).CBox2).addItem(str); available = findAvailList(tableNames[2]); for(String str : available) (((NotSavedInnerBox)(box[p-1][d-1])).CBox3).addItem(str); pTTPanel.add(((NotSavedInnerBox)(box[p-1][d-1])).getPanel()); } else { int number =0; box[p-1][d-1] = new SavedInnerBox(); (((SavedInnerBox)(box[p-1][d-1])).btn4).addActionListener(addListener(((SavedInnerBox)(box[p-1][d-1])), d, p, tableName, idName, tableNames)); System.out.println("rSet.next() count:"+ (++number)); (((SavedInnerBox)(box[p-1][d-1])).btn1).setText(rSet.getString(1)); (((SavedInnerBox)(box[p-1][d-1])).btn2).setText(rSet.getString(2)); (((SavedInnerBox)(box[p-1][d-1])).btn3).setText(rSet.getString(3)); pTTPanel.add(((SavedInnerBox)(box[p-1][d-1])).getPanel()); } } } public ActionListener addListener(final SavedInnerBox box, final int d, final int p, final String tableName, final String idName, final String[] tableNames) {//when edit button is pressed in innerBox return(new ActionListener() { public void actionPerformed(ActionEvent event) { String qry = "delete from tt where "+ "(day = "+d+") and (period = "+p+") and ("+tableName.charAt(0)+"id = "+idName+")"; try { System.out.println("Query for edit btn: "+qry); stmt.executeUpdate(qry); (Page2nd.btnFoot).setText("Enter the new values.."); } catch(SQLException err) { System.out.println("Edit button "+err.toString()); (Page2nd.btnFoot).setText("Try again.."); } } }); } public ActionListener addListener(final NotSavedInnerBox box, final int d, final int p, final String tableName, final String idName, final String[] tableNames) {//when save button is pressed in innerBox return(new ActionListener() { public void actionPerformed(ActionEvent event) { if(((box.CBox1).getSelectedIndex() != 0) && ((box.CBox2).getSelectedIndex() != 0) && ((box.CBox3).getSelectedIndex() != 0)) { String qry = "insert into tt values(day, period, "+ (tableName.charAt(0))+"id, "+(tableNames[0].charAt(0))+"id, "+(tableNames[1].charAt(0))+"id, "+(tableNames[2].charAt(0))+"id "+ "set("+d+", "+p+", "+idName+", "+((box.CBox1).getSelectedItem())+", "+((box.CBox2).getSelectedItem())+", "+((box.CBox3).getSelectedItem())+")"; try { System.out.println("Query for save btn: "+qry); stmt.executeUpdate(qry); (Page2nd.btnFoot).setText("Enter the new values.."); } catch(SQLException err) { System.out.println("Save button "+err.toString()); (Page2nd.btnFoot).setText("Try again.."); } } else (Page2nd.btnFoot).setText("Try again.."); } }); } public JPanel getPanel() { return(pTTPanel); } public ResultSet isEntrySaved(int d, int p, String tableName, String idName, String[] tableNames)// throws SQLException { ResultSet rset; String qry = "select "+tableNames[0].charAt(0)+"id, "+ tableNames[1].charAt(0)+"id, "+ tableNames[2].charAt(0)+"id from tt"+ " where ("+tableName.charAt(0)+"id = "+"'"+idName+"') and "+ "(day = "+d+") and "+ "(period = "+p+")"; //System.out.println("Query in isEntrySaved() : "+qry); rset = null; try { rset = stmt.executeQuery(qry); //c.close(); } catch(SQLException e) { System.out.println("b.myError in isEntrySaved()"+e.toString()); } return(rset); } public static String[] findAvailList(String tableName) { //new myFrame(); String[] fullList = ParticularTT.initialOptions(tableName, "id"); int fullNo = ParticularTT.noOfItems; String[] busyList = ParticularTT.initialOptions("tt", tableName.charAt(0)+"id"); int busyNo = ParticularTT.noOfItems; String[] availList = null; int availNo = fullNo-busyNo +1; if(availNo <= 1) ; else { availList = new String[availNo]; int count = 0; availList[0] = "-Select "+tableName+"-"; boolean flag = true; for(String f : fullList) { flag = true; for(String b : busyList){ if(b.equals(f)) { flag = false; break; } } if(flag == true) { count++; availList[count] = f; } } } return(availList); } public static String[] initialOptions( String tname , String cname)//returns first item as -Select- { int noOfRows=0; String qry=new String("select count(*) from "+tname);//Execute queries and/or updates String[] CBoxOptions = null; try { rset=stmt.executeQuery(qry); while(rset.next()) { noOfRows = rset.getInt(1); ParticularTT.noOfItems = noOfRows; } CBoxOptions = new String[noOfRows+1]; qry=new String("select "+cname+" from "+tname);//Execute queries and/or updates rset=stmt.executeQuery(qry); CBoxOptions[0] = "-Select-";//"-Select "+tname+"-";// for(int count=1; rset.next(); count++) { CBoxOptions[count] = rset.getString(1); } } catch(Exception e) { System.out.println("2.)Error in InitialOptions "+e.toString()); } return (CBoxOptions); } static int noOfItems =0; JPanel pTTPanel; static JdbcConnection con; static Connection c=null; static Statement stmt=null; static ResultSet rset = null; } abstract class InnerBox { InnerBox() { myPanel.setLayout(new GridLayout(4,1)); myPanel.setBorder(new SoftBevelBorder(EtchedBorder.LOWERED)); //myPanel.setBorder(BorderFactory.createLoweredBevelBorder()); } public JPanel getPanel() { return(this.myPanel); } protected JPanel myPanel = new JPanel(); } class SavedInnerBox extends InnerBox { SavedInnerBox() { myPanel.add(btn1); myPanel.add(btn2); myPanel.add(btn3); myPanel.add(btn4); } public JLabel btn1 = new JLabel(); public JLabel btn2 = new JLabel(); public JLabel btn3 = new JLabel(); public JButton btn4 = new JButton("Edit"); } class NotSavedInnerBox extends InnerBox { NotSavedInnerBox() { myPanel.add(CBox1); myPanel.add(CBox2); myPanel.add(CBox3); myPanel.add(btn4); } public JComboBox CBox1 = new JComboBox(); public JComboBox CBox2 = new JComboBox(); public JComboBox CBox3 = new JComboBox(); public JButton btn4 = new JButton("Save"); } /* class GBC extends GridBagConstraints { public GBC(int gridX, int gridY, int gridWidth, int gridHeight) { this.gridx = gridX; this.gridy = gridY; this.gridwidth = gridWidth; this.gridheight = gridHeight; } public GBC setIpad(int x, int y) { this.ipadx = x; this.ipady = y; return(this); } public GBC setWeight(double weightX, double weightY) { this.weightx = weightX; this.weighty = weightY; return(this); } }*/